package com.frx.datastruct.chapterseven;

import java.util.ArrayList;

/**
 * Created by xurenfeng on 2019/3/5.
 * 用链表实现集合映射
 */
public class LinkedListMap<K,V> implements Map<K,V>{

    private class Node{

        public K key;
        public V val;
        public Node next;

        public Node(K key,V val,Node next){
            this.key = key;
            this.val = val;
            this.next = next;
        }

        public Node(K key,V val){
            this(key,val,null);
        }

        public Node(){
            this(null,null,null);
        }

        @Override
        public String toString(){
            return val.toString();
        }
    }

    private Node dummyHead;
    private int size;

    public LinkedListMap(){
        dummyHead = new Node();
        size = 0;
    }
    @Override
    public int getSize() {
        return size;
    }

    @Override
    public boolean isEmpty() {
        return size > 0;
    }

    private Node getNode(K key){
        Node cur = dummyHead.next;
        while (cur!=null){
            if (cur.key.equals(key)){
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }
    @Override
    public void add(K key, V val) {
        Node node = getNode(key);
        if(node == null){
            dummyHead.next = new Node(key,val,dummyHead.next);
            size++;
        } else {
            node.val = val;
        }
    }

    @Override
    public boolean contains(K key) {
        Node node = getNode(key);
        return node != null;
    }

    @Override
    public V get(K key) {
        Node node = getNode(key);
        return node == null ? null : node.val;
    }

    @Override
    public void set(K key, V newValue) {
        Node node = getNode(key);
        if (node == null){
            throw new IllegalArgumentException(key + "doesn't exist !");
        }
        node.val = newValue;
    }



    @Override
    public V remove(K key) {
        Node prev = dummyHead;
        while (prev.next != null){
            if (prev.next.key.equals(key)){
                break;
            }
            prev = prev.next;
        }
        if (prev != null){
            Node delNode = prev.next;
            prev.next = delNode.next;
            delNode.next = null;
            size --;
            return delNode.val;
        }

        return null;
    }


    public static void main(String[] args){
        //final String FILE_PATH = "D:\\Users\\My\\JavaDataStruct\\src\\com\\frx\\pride-and-prejudice.txt";
        final String FILE_PATH = "D:\\MyProject\\JavaDataStruct\\src\\com\\frx\\pride-and-prejudice.txt";

        System.out.println("pride-and-prejudice");
        ArrayList<String> words = new ArrayList<>();
        if (FileOperation.readFile(FILE_PATH,words)){
            System.out.println("pride-and-prejudice has total words: " + words.size());
            LinkedListMap<String,Integer> listMap = new LinkedListMap<>();
            for (String word : words){
                if (listMap.contains(word)){
                    listMap.set(word,listMap.get(word)+1);
                } else {
                    listMap.add(word,1);
                }
            }
            System.out.println("pride-and-prejudice has different words :" + listMap.getSize());

            System.out.println("frequency of pride is " + listMap.get("pride"));
            System.out.println("frequency of prejudice is " + listMap.get("prejudice"));
        }

    }
}
